package gov.pbc.nn.kjgl.software.dao;

import gov.pbc.nn.core.dao.AbstractJdbcDao;
import gov.pbc.nn.core.dao.PbcnnCoreJdbcDao;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.entity.Project;

import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.data.entity.EntityUtils;

@Repository
public class KjcShJdbcDao extends AbstractJdbcDao {

	@Resource(name = PbcnnCoreJdbcDao.BEAN_ID)
	private PbcnnCoreJdbcDao pbcnnCoreJdbcDao;

	public Project loadProject(String id) {
		String sql = "SELECT\n" + "	T.*\n" + "FROM\n" + "	(\n" + "		SELECT\n" + "			A.*, B.FILE_NAME_ AS YWXQS_\n"
				+ "		FROM\n" + "			t_project AS A\n"
				+ "		LEFT JOIN t_pfile AS B ON a.BUSINESS_REQUIREMENTS_ID_ = b.ID_\n" + "		WHERE\n"
				+ "			A.INVALID_ = 0\n AND A.ID_=:id" + "	) T\n" + "WHERE\n" + "	1 = 1";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		List<Project> ls = this.getNamedParameterJdbcTemplate().query(sql, params, new RowMapper<Project>() {
			/*
			 * (non-Javadoc)
			 * 
			 * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql
			 * .ResultSet , int)
			 */
			@Override
			public Project mapRow(ResultSet rs, int rowNum) throws SQLException {
				Project a = new Project();
				a.setId(rs.getString("ID_"));
				a.setCreateUser(rs.getString("CREATE_USER_"));
				a.setCreateDate(rs.getDate("CREATE_DATE_"));
				a.setUpdateDate(rs.getDate("UPDATE_DATE_"));
				a.setUpdateUser(rs.getString("UPDATE_USER_"));
				a.setInvalid(rs.getBoolean("INVALID_"));
				a.setName(rs.getString("NAME_"));
				a.setProjectDept(rs.getString("PROJECT_DEPT_"));
				a.setProjectTime(rs.getDate("PROJECT_TIME_"));
				a.setDeliveryTime(rs.getDate("DELIVERY_TIME_"));
				a.setBudgetProject(rs.getDouble("BUDGET_PROJECT_"));
				a.setBusinessRequirementsId(rs.getString("BUSINESS_REQUIREMENTS_ID_"));
				a.setCallUser(rs.getString("CALL_USER_"));
				a.setCallUserPhone(rs.getString("CALL_USER_PHONE_"));
				a.setDeclareMeans(rs.getString("DECLARE_MEANS_"));
				a.setExternalParties(rs.getString("EXTERNAL_PARTIES_"));
				a.setKjDeptFirstOpinion(rs.getString("KJ_DEPT_FIRST_OPINION_"));
				a.setKjDirectorApproval(rs.getString("KJ_DIRECTOR_APPROVAL_"));
				a.setOtherId(rs.getString("OTHER_ID_"));
				a.setProjectBackground(rs.getString("PROJECT_BACKGROUND_"));
				a.setProjectContent(rs.getString("PROJECT_CONTENT_"));
				a.setProjectTarget(rs.getString("PROJECT_TARGET_"));
				a.setProjectType(rs.getString("PROJECT_TYPE_"));
				a.setTagHeadBank(rs.getInt("TAG_HEAD_BANK_"));
				a.setTagProject(rs.getInt("TAG_PROJECT_"));
				a.setXtztjsfaId(rs.getString("XTZTJSFA_ID_"));
				a.setXxhxmjysId(rs.getString("XXHXMJYS_ID_"));
				a.setYwDirectorApproval(rs.getString("YW_DIRECTOR_APPROVAL_"));
				a.setYwxqfxbgId(rs.getString("YWXQFXBG_ID_"));
				a.setDeptName(rs.getString("DEPT_NAME_"));
				a.setZgywhld(rs.getString("ZGYWHLD_"));
				a.setYwzgDept(rs.getString("YWZG_DEPT_"));
				a.setYwzgDeptId(rs.getString("YWZG_DEPT_ID_"));
				a.setZgywhldUsername(rs.getString("ZGYWHLD_USERNAME_"));
				a.setState(rs.getInt("STATE_"));
				a.setXqtcDept(rs.getString("XQTC_DEPT_"));
				a.setXqtcDeptId(rs.getString("XQTC_DEPT_ID_"));
				a.setHldqz(rs.getInt("HLDQZ_"));
				a.setQubie(rs.getString("QUBIE_"));
				try {
					a = EntityUtils.toEntity(a);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				EntityUtils.setValue(a, "ywxqs", rs.getString("YWXQS_"));
				return a;
			}
		});
		if (ls.size() > 0) {
			return ls.get(0);
		} else {
			return null;
		}

	}
}
